package com.android.equipment.ui.device

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.android.equipment.R
import com.android.equipment.adapter.DeviceManagerAdapter
import com.android.equipment.base.BaseActivity
import com.android.equipment.dao.Device
import com.android.equipment.dao.operate.DaoData
import com.android.equipment.dao.operate.DaoDevice
import com.android.equipment.databinding.ActivityDeviceManagerBinding
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.OnTitleBarListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.lxj.xpopup.interfaces.OnSelectListener

class DeviceManagerActivity : BaseActivity<ActivityDeviceManagerBinding>() {
    lateinit var deviceManagerAdapter: DeviceManagerAdapter

    private var deviceList: List<Device>? = null
    override fun setLayoutContent(): ActivityDeviceManagerBinding {
        return ActivityDeviceManagerBinding.inflate(layoutInflater)
    }

    @Throws(Exception::class)
    override fun initView(savedInstanceState: Bundle?) {
        initRecycler()
    }

    override fun initData() {
    }

    override fun initEvent() {
        super.initEvent()
        binding.mTitleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(v: View) {
                mSwipeBackHelper.backward()
            }

            override fun onTitleClick(v: View) {
            }

            override fun onRightClick(v: View) {
                mSwipeBackHelper.forward(EditDeviceActivity::class.java)
            }
        })
    }

    private fun refreshData() {
        deviceList = DaoDevice.getInstance().notDelDevice
        deviceList?.let {
            deviceManagerAdapter.setList(it)
        }

    }

    private fun initRecycler() {
        deviceManagerAdapter = DeviceManagerAdapter(R.layout.item_device_manager)
        binding.rvDeviceManager.adapter = deviceManagerAdapter


        deviceManagerAdapter.addChildClickViewIds(R.id.tv_clean_data,R.id.tv_look_data)
        deviceManagerAdapter.setOnItemChildClickListener { adapter, view, position ->

            when(view.id){
                R.id.tv_look_data->{
//                    val allData = DaoData.getInstance()
//                        .getAllDataByDeviceCode(deviceManagerAdapter.getItem(position).deviceCode)
//                    if (allData.isNotEmpty()){
//                        XPopup.Builder(this@DeviceManagerActivity)
//                            .asCenterList("设备数据",allData.map { it.txt_json }.toTypedArray(), object : OnSelectListener{
//                                override fun onSelect(position: Int, text: String?) {
//                                    // 获取系统剪贴板
//                                    val clipboard = mContext?.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
//                                    // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）,其他的还有
//                                    val clipData = ClipData.newPlainText(null, text)
//                                    // 把数据集设置（复制）到剪贴板
//                                    clipboard.setPrimaryClip(clipData)
//                                    ToastUtils.showShort("复制成功!")
//                                }
//
//                            }).show()
//                    }

                    val deviceCode = deviceManagerAdapter.getItem(position).deviceCode
                    val intent = Intent(this@DeviceManagerActivity, DeviceDataActivity::class.java)
                    intent.putExtra("deviceCode",deviceCode)
                    mSwipeBackHelper.forward(intent)

                }

                R.id.tv_clean_data -> {
                    XPopup.Builder(this@DeviceManagerActivity)
                        .asConfirm("提示","确定要清除该设备相关的数据吗?"
                        ) {
                            DaoData.getInstance().deleteDataByDeviceCode(deviceManagerAdapter.getItem(position).deviceCode)
                            ToastUtils.showShort("删除成功")
                        }.show()
                }
            }

        }

        deviceManagerAdapter.setOnItemLongClickListener { adapter, view, position ->
            val device = deviceManagerAdapter!!.getItem(position)
            XPopup.Builder(this@DeviceManagerActivity)
                .atView(view) // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(
                    arrayOf<String>("修改", "删除"),
                    intArrayOf()
                ) { position, text ->
                    if (position == 0) {
                        val intent =
                            Intent(this@DeviceManagerActivity, EditDeviceActivity::class.java)
                        intent.putExtra("id", device.id)
                        mSwipeBackHelper.forward(intent)
                    } else {
                        DaoDevice.getInstance().deleteDevice(device)
                        refreshData()
                    }
                }
                .show()
            false
        }
    }

    override fun setStatusBar() {
        ImmersionBar.with(this)
            .fullScreen(true)
            .statusBarColor(R.color.F8F9FA)
            .statusBarDarkFont(true)
            .titleBar(binding.mTitleBar)
            .init()
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }
}
